package com.iweibo.ui;

import java.util.List;

import com.iweibo.R;
import com.iweibo.adapter.UserInfoAdapter;
import com.iweibo.bean.UserInfo;
import com.iweibo.interfaces.IWeiboActivity;
import com.iweibo.service.UserInfoService;
import com.iweibo.util.ListTools;
import com.iweibo.util.SharedPreferencesTools;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class LoginActivity extends Activity implements IWeiboActivity {
	private static final String TAG = "LoginActivity";
	private UserInfoService service = new UserInfoService(this);
	private List<UserInfo> listUsers = null;
	// 弹出用户选择框
	private Dialog dialog = null;
	// 用户信息
	private UserInfo userInfo = null;

	private ProgressDialog progressDialog = null;
	
	/**由授权页面跳转*/
	public static int isAuth = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		userInfo = SharedPreferencesTools.getUserData(this);
		// 授权新用户按钮
		Button btnAddAccount = (Button) findViewById(R.id.btn_add_account);
		btnAddAccount.setOnClickListener(addAccountOnClick);
		// 选择用户按钮
		Button btnUserSelected = (Button) findViewById(R.id.btn_user_selected);
		btnUserSelected.setOnClickListener(userSelectedOnClick);
		// 用户登录按钮
		Button btnUserLogin = (Button) findViewById(R.id.btn_login);
		btnUserLogin.setOnClickListener(userLoginOnClick);
		
		listUsers = service.findAllUserInfo();
		if (null != userInfo) {
			showProgressDialog(getResources().getText(R.string.logining));
			jump();
			closeProgressDialog();
		} else {
			init();
		}
	}

	/**
	 * 跳转到首页
	 */
	private void jump() {
		Intent intent = new Intent(this, MainActivity.class);
		if (null != userInfo) {
			//将用户信息传递到MainActivity
			intent.putExtra("user_id", userInfo.getUserId());
			intent.putExtra("user_name", userInfo.getUserName());
			intent.putExtra("token", userInfo.getToken());
			intent.putExtra("expires_in", userInfo.getExpiresIn());
		}
		
		startActivity(intent);
	}

	/**
	 * 显示正在登录进度框
	 * 
	 * @param msg
	 *            提示信息
	 */
	private void showProgressDialog(CharSequence msg) {
		if (null == progressDialog) {
			progressDialog = new ProgressDialog(this);
			progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
		progressDialog.setMessage(msg);
		progressDialog.show();
	}

	/**
	 * 关闭正在登录进度框
	 */
	private void closeProgressDialog() {
		if(null != progressDialog) {
			progressDialog.dismiss();
		}
	}

	@Override
	public void init() {
		if (ListTools.isEmpty(listUsers)) {
			startAuthActivity();
		}
	}

	/**
	 * 打开授权提示框
	 */
	private void startAuthActivity() {
		Intent intent = new Intent(this, AuthActivity.class);
		startActivity(intent);
	}

	// 显示用户列表事件
	private OnClickListener userSelectedOnClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {

			View view = View.inflate(LoginActivity.this,
					R.layout.user_seleted_dialog, null);
			dialog = new Dialog(LoginActivity.this, R.style.user_seleted_dialog);
			dialog.setContentView(view);
			dialog.show();
			ListView lvUserList = (ListView) view
					.findViewById(R.id.lv_user_list);
			UserInfoAdapter userInfoAdapter = new UserInfoAdapter(
					LoginActivity.this, listUsers);
			lvUserList.setAdapter(userInfoAdapter);
			lvUserList.setOnItemClickListener(userItemClick);
		}
	};

	// 点击选择用户列表事件
	private OnItemClickListener userItemClick = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int arg2,
				long id) {
			ImageView imgHead = (ImageView) view
					.findViewById(R.id.img_user_head);
			TextView txtUserName = (TextView) view
					.findViewById(R.id.txt_user_name);
			TextView txtUserNameLogin = (TextView) findViewById(R.id.txt_user_name_login);
			txtUserNameLogin.setText(txtUserName.getText());
			ImageView imgUserHead = (ImageView) findViewById(R.id.img_user_head_login);
			imgUserHead.setImageDrawable(imgHead.getDrawable());
			//根据用户id获取用户信息
			userInfo = service.queryUserInfoById(id);
			dialog.dismiss();
		}

	};

	// 点击授权按钮事件
	private OnClickListener addAccountOnClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			startAuthActivity();
		}
	};
	
	private OnClickListener userLoginOnClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			loginUser();
		}
	};
	
	/**
	 * 用户登录方法
	 */
	private void loginUser() {
		showProgressDialog(getResources().getText(R.string.logining));
		boolean res = SharedPreferencesTools.saveData(LoginActivity.this, userInfo);
		if (res) {
			Log.d(TAG, getResources().getText(R.string.save_user_file_success).toString());
			jump();
		} else {
			Toast.makeText(this, getResources().getText(R.string.login_fail), Toast.LENGTH_LONG).show();
			Log.d(TAG, getResources().getText(R.string.save_user_file_fail).toString());
		}
		closeProgressDialog();
	}

	@Override
	public void refresh(Object... params) {
	}

}
